<?php

/*
 * 权限控制
 */
function checkAccess($module_name = '', $action_name = '', $mid = 0)
{
    if (!$mid) {
        $mid = M('Menu')->where(array('deleted' => 0, 'model' => $module_name, 'action' => $action_name))->getField('id');
    }
    //检查用户权限
    $User = session('BEUSER');
    //Admin
    if ($User['usergroup']) {
        if ($User['usergroup'] == 1) { //Admin
            return true;
        } else {
            if ($mid > 1 && !M('Access')->where(array('group_id' => $User['usergroup'], 'menu_id' => $mid))->count()) {
                return false;
            } else {
                return true;
            }
        }
    } else {
        return false;
    }
}

function isImage($file_name){
    $ext = array_pop(explode('.', $file_name));

    if (in_array($ext, array('jpg', 'png', 'bmp', 'gif'))){
        return true;
    } else {
        return false;
    }
}


/**
 * 字符串截取，支持中文和其他编码
 * @static
 * @access public
 * @param string $str 需要转换的字符串
 * @param string $start 开始位置
 * @param string $length 截取长度
 * @param string $charset 编码格式
 * @param string $suffix 截断显示字符
 * @return string
 */
function msubstr($str, $start=0, $length, $charset="utf-8", $suffix=true){
    if(function_exists("mb_substr")){
        if($suffix && mb_strlen($str,$charset)>$length){
            return mb_substr($str, $start, $length, $charset)."...";
        }else{
            return mb_substr($str, $start, $length, $charset);
        }
    }elseif(function_exists('iconv_substr')) {
        if($suffix && mb_strlen($str,$charset)>$length){
            return iconv_substr($str,$start,$length,$charset)."...";
        }else{
            return iconv_substr($str,$start,$length,$charset);
        }
    }
    $re['utf-8']   = "/[\x01-\x7f]|[\xc2-\xdf][\x80-\xbf]|[\xe0-\xef][\x80-\xbf]{2}|[\xf0-\xff][\x80-\xbf]{3}/";
    $re['gb2312'] = "/[\x01-\x7f]|[\xb0-\xf7][\xa0-\xfe]/";
    $re['gbk']    = "/[\x01-\x7f]|[\x81-\xfe][\x40-\xfe]/";
    $re['big5']   = "/[\x01-\x7f]|[\x81-\xfe]([\x40-\x7e]|\xa1-\xfe])/";
    preg_match_all($re[$charset], $str, $match);
    $slice = join("",array_slice($match[0], $start, $length));
    if($suffix && mb_strlen($str,$charset)>$length){
        return $slice."...";
    }else{
        return $slice;
    }
}
/**
 * 二维数组去掉重复值
 * @param array $array2D
 * @return multitype:
 */
function array_unique_fb($array2D){
	foreach ($array2D as $v){
		$v=join(',',$v);//降维,也可以用implode,将一维数组转换为用逗号连接的字符串
		$temp[]=$v;
	}
	$temp=array_unique($temp);//去掉重复的字符串,也就是重复的一维数组
	foreach ($temp as $k => $v){
		$temp[$k]=explode(',',$v);//再将拆开的数组重新组装
	}
	return $temp;
}

/**
 * 生成number
 * @param       int         $code
 * @param       int         $max
 * @param       string      $prefix
 * @return      string
 */
function getNumber($code="",$max=5,$prefix="")
{
	$l = '';
	if($code==''){
		for($i=1;$i<=$max;$i++)
		{
			$l .="0";
		}
		$n =  intval("1".$l);
		$code = rand(1,$n);
	}
	$num = $prefix.date('ymd').sprintf('%0'.$max.'d', $code);
	return $num;
}

?>